package org.dalmatian.common.mybatis.aspect;

import org.dalmatian.common.mybatis.annotation.DataPermission;
import org.dalmatian.common.mybatis.helper.DataPermissionHelper;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.core.aspect.Interceptor;
import org.noear.solon.core.aspect.Invocation;

/**
 * 数据权限处理 (Solon 版本)
 *
 * @author Lion Li
 */
@Slf4j
public class DataPermissionInterceptor implements Interceptor {


    @Override
    public Object doIntercept(Invocation inv) throws Throwable {
        DataPermission anno = inv.getMethodAnnotation(DataPermission.class);
        if (anno == null) {
            anno = inv.getTargetAnnotation(DataPermission.class);
        }
        if (anno == null) {
            return inv.invoke();
        } else {
            Object result ;
            try{
            // 处理请求前执行
            DataPermissionHelper.setPermission(anno);
            result = inv.invoke();
            DataPermissionHelper.removePermission();
            return result;
                //处理完请求后执行
            }catch (Exception  e){
                //異常處理
                DataPermissionHelper.removePermission();
            }
          return null;
        }
    }
}